package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;

import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.util.MD5;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Li
 * @Date 2023/7/6 15:20
 */
@RestController
@Api(tags = "用户管理", value = "用户Open api 入口")
@RequestMapping("/back/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     *
     * @param userModel
     * @return
     */
    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询所有用户", response = UserModel.class, httpMethod = "GET")
    public ResultDto findAll(UserModel userModel) {
        IPage<UserModel> page = new Page<>(userModel.getPage(), userModel.getLimit());
        QueryWrapper<UserModel> queryWrappers = query(userModel);
        IPage<UserModel> iPage = userService.page(page, queryWrappers);
        return ResultDto.builder()
                .code(0)
                .data(iPage.getRecords())
                .count((int) iPage.getTotal())
                .build();
    }

    @PostMapping("/addUser")
    @PreAuthorize("hasAnyAuthority('user:add')")//权限
    @ApiOperation(value = "新增用户", response = UserModel.class, httpMethod = "POST")
    public ResultDto addUser(UserModel userModel) {
        userModel.setPassword(MD5.encoderByMd5(userModel.getPassword()));
        //返回代码信息，数据条数
        return userService.add(userModel);
    }

    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询用户", response = UserModel.class, httpMethod = "GET")
    public ResultDto findById(UserModel userModel) {
        UserModel user = userService.getById(userModel.getId());
        return ResultDto.builder()
                .code(0)
                .data(user)
                .build();
    }

    @PostMapping("/updateUser")
    @PreAuthorize("hasAnyAuthority('user:update')")//权限
    @ApiOperation(value = "修改用户", response = UserModel.class, httpMethod = "POST")
    public ResultDto updateUser(UserModel userModel) {
        userService.updateById(userModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功!")
                .build();
    }

    @PostMapping("/deleteUser")
    @PreAuthorize("hasAnyAuthority('user:delete')")//权限
    @ApiOperation(value = "删除用户", response = UserModel.class, httpMethod = "POST")
    public ResultDto deleteUser(UserModel userModel) {
        userService.removeById(userModel.getId());
        return ResultDto.builder()
                .code(0)
                .msg("删除成功！")
                .build();
    }

    @PostMapping("/updatePassword")
    @PreAuthorize("hasAnyAuthority('update:password')")//权限
    @ApiOperation(value = "修改密码", response = UserModel.class, httpMethod = "POST")
    public ResultDto updatePassword(UserModel userModel) {

        return userService.updatePassword(userModel);
    }

    @PostMapping("/resetPassword")
    @PreAuthorize("hasAnyAuthority('reset:password')")//权限
    @ApiOperation(value = "重置密码", response = UserModel.class, httpMethod = "POST")
    public ResultDto resetPassword(UserModel userModel) {
        return userService.resetPassword(userModel);

    }

    public QueryWrapper<UserModel> query(UserModel userModel) {
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(userModel.getUsername() == null), "username", userModel.getUsername());
        queryWrapper.like(!(userModel.getSex() == null), "sex", userModel.getSex());
        if (userModel.getStartAge() != null && userModel.getEndAge() == null) {
            queryWrapper.ge("age", userModel.getStartAge());
        } else if (userModel.getEndAge() != null && userModel.getStartAge() == null) {
            queryWrapper.le("age", userModel.getEndAge());
        } else {
            queryWrapper.between(!(userModel.getStartAge() == null && userModel.getEndAge() == null), "age", userModel.getStartAge(), userModel.getEndAge());
        }

        queryWrapper.like(!(userModel.getPhone() == null), "phone", userModel.getPhone());
        queryWrapper.like(!(userModel.getAddress() == null), "address", userModel.getAddress());
        queryWrapper.like(!(userModel.getEmail() == null), "email", userModel.getEmail());
        queryWrapper.eq(userModel.getEnable() != null && userModel.getEnable() != -1, "enable", userModel.getEnable());
        return queryWrapper;
    }

}


